package com.lecoauto.e02.activity;

import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.lecoauto.e02.databinding.MainActivityBinding;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;


public class MainActivity extends AppCompatActivity {
    public SharedPreferences preferences;
    public MainActivityBinding binding;
    private static final String STATE_PROVIDER_KEY = "activity_state";
    private static final String STATE_CAR_CODE = "car_code";
    private String carCode;

    public MainActivity() {
        getSavedStateRegistry().registerSavedStateProvider(STATE_PROVIDER_KEY, () -> {
            Bundle bundle = new Bundle();
            if (carCode != null) {
                bundle.putString(STATE_CAR_CODE, carCode);
            }
            return bundle;
        });

        addOnContextAvailableListener(context -> {
            // 恢复状态
            Bundle restoredState = getSavedStateRegistry().consumeRestoredStateForKey(STATE_PROVIDER_KEY);
            if (restoredState != null) {
                carCode = restoredState.getString(STATE_CAR_CODE);
            }
        });
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        preferences = getSharedPreferences("e02", 0);
        binding = MainActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        checkActivated();

        binding.info.setText("模块介绍：用于修复安卓系统9.0车机画中画不能点击的问题。");
        binding.about.setText("@乐酷出品 LecoAuto.com");

        StringBuilder sb = new StringBuilder("机器代码：");
        String carCode = generateCarCode();
        sb.append(carCode);

        binding.carCode.setText(sb.toString());
        binding.submit.setOnClickListener(v -> {
            if (binding.code.getText().toString().isEmpty()) {
                Toast.makeText(this, "请输入激活码！", Toast.LENGTH_SHORT).show();
            } else {
                preferences.edit().putBoolean("is_activated", true).apply();
                checkActivated();
            }
        });
    }

    private String generateCarCode() {
        if (carCode == null) {
            String string = Settings.Secure.getString(getContentResolver(), "android_id");
            if (TextUtils.isEmpty(string) || "9774d56d682e549c".equals(string)) {
                string = Build.BOARD + "," + Build.BRAND + "," + Build.DEVICE + "," + Build.MANUFACTURER + "," + Build.MODEL + "," + Build.PRODUCT;
            }
            try {
                byte[] digest = MessageDigest.getInstance("SHA-256").digest(string.getBytes(StandardCharsets.UTF_8));
                StringBuilder sb2 = new StringBuilder();
                for (byte b : digest) {
                    String hexString = Integer.toHexString(b & 255);
                    if (hexString.length() == 1) {
                        sb2.append('0');
                    }
                    sb2.append(hexString);
                }
                string = sb2.toString();
            } catch (Exception e) {
            }
            carCode = string;
        }

        return carCode;
    }

    public void checkActivated() {
        if (preferences.getBoolean("is_activated", false)) {
            binding.status.setText("模块状态：已激活");
        } else {
            binding.status.setText("模块状态：未激活");
        }
    }

}
